﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using WFNetLib;
using WFNetLib.Forms;
using WFNetLib.GSMModem;
using System.Data;
using WFNetLib.ADO;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 自消费
{
    public partial class TaskThread
    {
        public SerialPort Com;
        public int lviIndex;
        public Thread TaskThreadIns = null;
        public static event intstrEventHandler TaskThreadEvent;//事件声明
        public Boolean bClose;
        public static int TaskGroup;
        deviceData device;
        CardData card;
        public CardTaskData cardTask;
        public string PortName;
        void OnAddLog(string strLog,int type)
        {
            if (TaskThreadEvent != null)
            {
                TaskThreadEvent(this, new intstrEventArgs(type,strLog));
            }
        }
        public TaskThread(deviceData d,int index)
        {
            bClose = false;
            device = d;
            lviIndex = index;
            Com = new SerialPort("COM" + d.commport.ToString(), d.baudrate);
            Com.RtsEnable = true;
#if CommunicationDebug
            commDF = new CommunicationDebugForm();
            //CommunicationDebugForm.IsClose = true;
            commDF.Show();
            //CommunicationDebugForm.Hide();
            commDF.Text = Com.PortName + "数据检测";
#endif
        }       
        public void TaskThreadStart()
        {
            TaskThreadIns = new Thread(new ThreadStart(TaskProc));
            TaskThreadIns.Name = Com.PortName;
            PortName = Com.PortName;
            TaskThreadIns.Start();
            OnAddLog("任务开始",(int)TaskThreadEventType.Log);
        }        
        private void TaskProc()
        {            
            List<TaskData> taskList;         
            ATCommand atComm = new ATCommand(Com);
            try
            {
                Com.Open();
                while (true)
                {                    
                    card = InitDevice();
                    if (card == null)
                    {
                        //OnAddLog("无卡", (int)TaskThreadEventType.Log);
                        continue;
                    }
                    taskList = CheckTask();
                    OnAddLog("", (int)TaskThreadEventType.NewTask);
                    if (taskList.Count==0)
                    {
                        OnAddLog("完成所有任务", (int)TaskThreadEventType.Log);
                        //break;
                        continue;
                    }
                    OnAddLog("新卡", (int)TaskThreadEventType.Log);
                    foreach (TaskData td in taskList)
                    {
                        OnAddLog(td.taskname, (int)TaskThreadEventType.NewTask);
                        switch (td.type)
                        {
                            case 1:
                                cardTask = new CardTaskData();
                                cardTask.starttime = DateTime.Now;
                                cardTask.imei = device.imei;
                                cardTask.succeed = false;
                                cardTask.task_content = td.content;
                                cardTask.task_name = td.taskname;
                                cardTask.task_type = td.type;
                                cardTask.card_id = card.ID;
                                cardTask.commport = device.commport;
                                主题(td);
                                break;
                            default:
                                break;
                        }
                    }
                    Thread.Sleep(5000);
                }                
            }
            catch (InvalidOperationException ex)//端口被关闭,任务结束
            {
                OnAddLog("通道关闭", (int)TaskThreadEventType.Stop);
                //throw ex;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                OnAddLog("通道异常关闭，可能需要重启该通道设备", (int)TaskThreadEventType.Finish);
            }
            finally
            {
            }
        }
        List<TaskData> CheckTask()
        {
            List<TaskData> taskList = new List<TaskData>();
            OleDbDataReader reader = WFNetLib.ADO.AccessOP.ReaderExecuteSQL("select * from task where enabled=true and taskgroup_id=" + TaskGroup + " order by seqnum");
            while (reader.Read())
            {
                TaskData td = TaskDataFactory.Construct(reader);
                string sql;
                sql = "select * from CardTask where succeed=true and card_id='" + card.ID + "' and task_name='" + td.taskname + "'";
                DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql);
                if (dt.Rows.Count < td.RunTimes)
                {
                    td.RunTimes = td.RunTimes - dt.Rows.Count;
                    taskList.Add(td);
                }                
            }
            reader.Close();
            return taskList;
        }
    }
    public enum TaskThreadEventType
    {
        Finish = 0,
        Log = 1,
        CSQ =2,
        Error = 3,
        FinishTask=4,
        Stop=5,
        NewTask=6,
        TaskFail=7
    }
}
